JSGF.AudioSE = JSGF.inherit(JSGF.Object);

JSGF.AudioSE.prototype.init = function(file, count) {

	this.type = "JSGF.AudioSE";
	this.__audioBufferCount__ = 4;
	if (count != null) {
		this.__audioBufferCount__ = count;
	}
	var self = this;
	this.__audioList__ = new Array();
	for (var i = 0; i < this.__audioBufferCount__; i++) {
		this.__audioList__[i] = new Audio();
		this.__audioList__[i].no = i;
		this.__audioList__[i].isplay = false;
		this.__audioList__[i].preload = "auto";
		this.__audioList__[i].src = null;
		this.__audioList__[i].volume = 1.0;
		this.__audioList__[i].autoplay = false;
		this.__audioList__[i].loop = false;
		this.__audioList__[i].controls = false;
		var audiounit = this.__audioList__[i]
		eval("this.__audioList__[i].addEventListener('ended', function(){self.ended("+i+");}, false);");
	}
	if (file != null) {
		this.setFile(file);
	}

}

JSGF.AudioSE.prototype.ended = function(no) {
	this.__audioList__[no].pause();
	//this.__audioList__[no].currentTime=0;
	this.__audioList__[no].isplay = false;

}


////////////////////////////////////////
//Public
////////////////////////////////////////

JSGF.AudioSE.prototype.setFile = function(file) {
	var fileArray = file.split(".");
	var fileKind = "." + fileArray[fileArray.length - 1].toLowerCase();
	if (fileKind == ".ogg" || fileKind == ".mp3" || fileKind == ".wav" || fileKind == ".mp4") {
		for (var i = 0; i < this.__audioList__.length; i++) {
			this.__audioList__[i].src = file;
		}
	} else {
		for (var i = 0; i < this.__audioList__.length; i++) {
			var canPlayOgg = ("" != this.__audioList__[i].canPlayType("audio/ogg"));
			var canPlayMp3 = ("" != this.__audioList__[i].canPlayType("audio/mpeg"));
			var canPlayMp4 = ("" != this.__audioList__[i].canPlayType("audio/aac"));
			if(canPlayOgg) {
				this.__audioList__[i].src = file + ".ogg";
			} else if(canPlayMp3) {
				this.__audioList__[i].src = file + ".mp3";
			} else if(canPlayMp4) {
				this.__audioList__[i].src = file + ".mp4";
			}
		}
	}
}

JSGF.AudioSE.prototype.setVolume = function(vol) {
	if (vol >= 0.1 && val <= 1.0) {
		for (var i = 0; i < this.__audioList__.length; i++) {
			this.__audioList__[i].volume = vol;
		}
	}
}

JSGF.AudioSE.prototype.getVolume = function() {
	return this.__audioList__[0].volume;
}

JSGF.AudioSE.prototype.play = function(start) {
	var ret = 0;
	for (var i = 0; i < this.__audioList__.length; i++) {
		if (this.__audioList__[i].isplay == false) {
			if (start == null || start == true) {
				//this.__audioList__[i].currentTime = 0;
			}
			this.__audioList__[i].isplay = true;
			this.__audioList__[i].play();
			ret = i;
			break;
		}
	}
	return ret;
}

JSGF.AudioSE.prototype.stop = function(no) {
	if (no == null) {
		this.__audioList__[no].pause();
		this.__audioList__[no].currentTime=0;
		this.__audioList__[no].isplay = false;
	} else {
		for (var i = 0; i < this.__audioList__.length; i++) {
			this.__audioList__[i].pause();
			this.__audioList__[i].currentTime=0;
			this.__audioList__[i].isplay = false;
		}
	}
}

////////////////////////////////////////
// definition by user
////////////////////////////////////////

